package com.ctg.itrdc.sysmgr.portal.service.aop;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.sysmgr.portal.api.dto.FuncCompDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.FuncMenuDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.OrganizationDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivilegeDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemRolesDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.delete.IDeleteStrategy;

@Aspect
@Component
@Order(2)
public class DeleteMethodAOP {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IDeleteStrategy deleteStrategy;

	@Pointcut(value = "execution(* deleteByPrimaryKey(..))"
			+ "|| execution(* batchDeleteByPrimaryKey(..))")
	private void pointcut() {
	}

	@Before(value = "pointcut() && args(args)")
	public void deleteRel(Object args) {
		if (args instanceof List) {
			List<?> dtoList = (List<?>) args;
			deleteListRel(dtoList);
		} else {
			deleteSingleRel(args);
		}
	}

	private void deleteSingleRel(Object args) {

		if (args instanceof SystemUserDTO) {
			deleteStrategy.deleteSystemUserRel((SystemUserDTO) args);
		} else if (args instanceof SystemPostDTO) {
			deleteStrategy.deleteSystemPostRel((SystemPostDTO) args);
		} else if (args instanceof SystemRolesDTO) {
			deleteStrategy.deleteSystemRolesRel((SystemRolesDTO) args);
		} else if (args instanceof PrivilegeDTO) {
			deleteStrategy.deletePrivilegeRel((PrivilegeDTO) args);
		} else if (args instanceof FuncMenuDTO) {
			deleteStrategy.deleteFuncMenuRel((FuncMenuDTO) args);
		} else if (args instanceof FuncCompDTO) {
			deleteStrategy.deleteFuncCompRel((FuncCompDTO) args);
		}
	}

	private void deleteListRel(List<?> dtoList) {

		if (CollectionUtils.isEmpty(dtoList))
			return;
		Object dto = dtoList.get(0);

		if (dto instanceof SystemUserDTO) {
			for (Object args : dtoList)
				deleteStrategy.deleteSystemUserRel((SystemUserDTO) args);
		} else if (dto instanceof SystemPostDTO) {
			for (Object args : dtoList)
				deleteStrategy.deleteSystemPostRel((SystemPostDTO) args);
		} else if (dto instanceof SystemRolesDTO) {
			for (Object args : dtoList)
				deleteStrategy.deleteSystemRolesRel((SystemRolesDTO) args);
		} else if (dto instanceof PrivilegeDTO) {
			for (Object args : dtoList)
				deleteStrategy.deletePrivilegeRel((PrivilegeDTO) args);
		} else if (dto instanceof FuncMenuDTO) {
			for (Object args : dtoList)
				deleteStrategy.deleteFuncMenuRel((FuncMenuDTO) args);
		} else if (dto instanceof FuncCompDTO) {
			for (Object args : dtoList)
				deleteStrategy.deleteFuncCompRel((FuncCompDTO) args);
		}
	}
}
